package cn.itcast.bos.web.action;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import com.opensymphony.xwork2.ActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: SOS
 * @create: 2018-02-26 19:13
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

    @Action(value = "promotion_pageQuery", results = {@Result(name = SUCCESS, type = "json"),
            @Result(name = "input", location = "./index.html", type = "redirect")})
    public String promotionQuery() {

        PageBean<Promotion> pageBean = WebClient.
                create(Constants.BOS_MANAGEMENT_URL + "/services/promotionService/pageQuery?page=" + page + "&rows=" + rows).
                accept(MediaType.APPLICATION_JSON).get(PageBean.class);
        ActionContext.getContext().getValueStack().push(pageBean);
        return SUCCESS;
    }

    /**
     * 商品详情页面
     *
     * @return
     */
    @Action(value = "promotion_showDetail")
    public String showDetail() throws IOException, TemplateException {
        // 先判断 id 对应html 是否存在，如果存在 直接返回
        String htmlRealPath = ServletActionContext.getServletContext()
                .getRealPath("/freemarker");
        //D:\Develop\workspace\ideabos2.0\bos_fore\src\main\webapp\freemarker
        File htmlFile = new File(htmlRealPath + "/" + model.getId() + ".html");

        // 如果html文件不存在 ，查询数据库，结合freemarker模板生成 页面
        if (!htmlFile.exists()) {
            Configuration configuration = new Configuration(
                    Configuration.VERSION_2_3_22);
            configuration.setDirectoryForTemplateLoading(new File(
                    ServletActionContext.getServletContext().getRealPath(
                            "/WEB-INF/freemarker_templates")));
            // 获取模板对象
            Template template = configuration
                    .getTemplate("promotion_detail.ftl");

            // 动态数据
            Promotion promotion = WebClient
                    .create(Constants.BOS_MANAGEMENT_URL
                            + "/services/promotionService/promotion/"
                            + model.getId()).accept(MediaType.APPLICATION_JSON)
                    .get(Promotion.class);
            Map<String, Object> parameterMap = new HashMap<String, Object>();
            parameterMap.put("promotion", promotion);

            // 合并输出
            template.process(parameterMap, new OutputStreamWriter(
                    new FileOutputStream(htmlFile), "utf-8"));
        }
        //存在的话直接访问,通过输入流输入到当前页面
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        FileUtils.copyFile(htmlFile, ServletActionContext.getResponse().getOutputStream());

        return NONE;
    }

}
